/*
 * @Author: soso
 * @Date: 2022-01-08 17:59:58
 * @LastEditTime: 2022-03-14 15:01:27
 * @LastEditors: Please set LastEditors
 * @Description: 同步文件客户端主程序
 * @FilePath: /sync-client/main.go
 */

package main

import (
	"gomeshcli/comm"
	"gomeshcli/comm/defines"
	"gomeshcli/notify"
	"gomeshcli/sconfig"
	"strings"

	"gitee.com/kimsoso/go-utils/sonotify"
	"github.com/fsnotify/fsnotify"
)

var (
	sontf *sonotify.Watch
)

// 含有无用元素
func hasIgnoreConditions(act fsnotify.Op, path string) bool {
	return act == fsnotify.Chmod || path == "" || strings.HasSuffix(path, defines.DirFlagFile) || strings.HasSuffix(path, ".DS_Store")
}

func main() {
	var err error
	sontf, err = sonotify.NewDirWatch()
	if err != nil {
		panic(err)
	}
	defer sontf.Close()

	done := make(chan bool)

	ntfs := notify.NewNotifies()
	sontf.WatchDir(sconfig.SConfig.WatchDir, func(act fsnotify.Op, filePath string) {
		if !hasIgnoreConditions(act, filePath) {
			ntfs.Add(act.String(), filePath)
		}
	})

	// sync local all files
	comm.SyncDir(".")

	go func() {
		for {
			ntfs.ProcessNtfs()
			ntfs.Sleep()
		}
	}()

	// run a mini http server
	// websvr.RunWebSvr(sconfig.SConfig.HttpPort, sconfig.SConfig.WatchDir)

	<-done
}
